<?php
namespace app\admin\controller;

use think\Loader;
use think\Request;
use app\common\controller\Base;
use app\common\model\CustomerCase as CustomerCaseModel;
use app\common\model\CaseCat as CaseCatModel;
use app\common\tool\Tool;
use app\common\model\Product as ProductModel;

/**
 * 无法使用Case 因为是关键词
 * Class CustomerCase
 * @package app\admin\controller
 */
class CustomerCase extends Base
{
    public function index(){

        $customerCases = CustomerCaseModel::order('id desc')->paginate(20);

        return view('',['customerCases'=>$customerCases]);
    }


    public function add(){

        $caseCats = CaseCatModel::all();
        $caseCats = collection($caseCats)->toArray();
        $caseCats = Tool::changeArrayKey($caseCats);

        $caseCats = Tool::findSub($caseCats,0);

        return view('',['caseCats'=>$caseCats]);
    }


    public function  create(Request $request){
        $data = $request->only(['title','desc','cover','cover_title','cover_desc','catid','content',]);

        $validate = Loader::validate('CustomerCaseValidate');
        $result = $validate-> scene('add')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }

        $customerCaseModel = new CustomerCaseModel();
        $customerCaseModel ->save($data);

        $this->success('添加成功');
    }


    public function edit(Request $request){

        $id = $request->param('id',0,'intval');

        if(!$id){
            $this->error('非法来源');
        }

        $customerCase = CustomerCaseModel::find($id);

        $caseCats = CaseCatModel::all();
        $caseCats = collection($caseCats)->toArray();
        $caseCats = Tool::changeArrayKey($caseCats);
        $caseCats = Tool::findSub($caseCats,0);


        return view('',['customerCase'=>$customerCase,'caseCats'=>$caseCats]);
    }

    public function update(Request $request){
        $id = $request -> param('id');
        $data = $request->only(['title','desc','cover','cover_title','cover_desc','catid','content',]);

        $validate = Loader::validate('CustomerCaseValidate');
        $result = $validate-> scene('edit')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }

        $customerCaseModel = CustomerCaseModel::find($id);
        $customerCaseModel ->save($data);

        $this->success('更新成功');
    }


    public function delete(Request $request){
        $id = $request -> param('id');
        CustomerCaseModel::destroy($id);
        $this->success("删除成功");
    }


    public function assoc(Request $request){
        $id = $request->param('id',0,'intval');
        $customerCase = CustomerCaseModel::find($id);


        if(!$customerCase){
            $this->error("非法渠道访问");
        }

        $assoc = json_decode($customerCase->assoc);
        if(!$assoc){
            $assoc = [];
        }

        $assocProducts = ProductModel::whereIn('id',$assoc)->select();

        return view('',['customerCase'=>$customerCase,'assocProducts'=>$assocProducts]);
    }

    public function forAssoc(Request $request){

        $productList = ProductModel::order('id desc')->paginate(5);

        return view('',['productList'=>$productList]);
    }


    public function addAssoc(Request $request){
        $id = $request->param('id',0,'intval');
        $assocId = $request->param('assocId',0,'intval');

        $customerCase = CustomerCaseModel::find($id);
        if(!$customerCase){
            $this->error("来源渠道非法");
        }
        $assoc  = json_decode($customerCase->assoc);

        $assocProduct = ProductModel::find($assocId);

        if(!$assoc){
            $assoc = [$assocId];
        }else{
            if(in_array($assocId,$assoc)){
                $this->success("操作成功",null,json_decode($assocProduct));
            }else{
                array_push($assoc,$assocId);
            }
        }

        $customerCase->assoc = json_encode($assoc);
        $customerCase->save();

        $this->success("操作成功",null,json_decode($assocProduct));
    }

    public function cancelAssoc(Request $request){
        $id = $request->param('id',0,'intval');
        $assocId = $request->param('assocId',0,'intval');

        $customerCase = CustomerCaseModel::find($id);
        if(!$customerCase){
            $this->error("来源渠道非法");
        }
        $assoc  = json_decode($customerCase->assoc);
        if(!$assoc){
            $this->success("操作成功");
        }else{
            $newAssoc = [];
            foreach($assoc as $val ){
                if($val != $assocId){
                    array_push($newAssoc,$val);
                }
            }
        }
        $customerCase->assoc = json_encode($newAssoc);
        $customerCase->save();


        $this->success("操作成功");
    }


    public function banner(Request $request){
        $id = $request->param('id',0,'intval');
        $customerCase = CustomerCaseModel::find($id);
        if(!$customerCase){
            $this->error("来源渠道非法");
        }

        return view('',['customerCase'=>$customerCase]);
    }


    public function updateBanner(Request $request){
        $id = $request->param('id',0,'intval');
        $banner = $request->param('banner','','trim');

        $customerCase = CustomerCaseModel::find($id);
        if(!$customerCase){
            $this->error("来源渠道非法");
        }

        $customerCase->banner = $banner;
        $customerCase->save();

        $this->success("操作成功");
    }
}



